using System.Collections.Generic;
using System.Linq;

namespace RayDen.Library.Entity.Scene
{
    public class MaterialInfoContainer : IMaterialProvider
    {
        private MaterialInfo[] materials;
        private MaterialInfo defaultMaterial;
        private Dictionary<string, int> map;

        public MaterialInfoContainer( MaterialInfo mt, IEnumerable<MaterialInfo> mats )
        {
            this.defaultMaterial = mt;
            this.map = new Dictionary<string, int>();
            this.materials = mats == null ? new []{mt} : mats.ToArray();
            for (int index = 0; index < this.materials.Length; index++)
            {
                var materialInfo = this.materials[index];
                this.map.Add(materialInfo.Name.ToLower(), index);
            }
        }

        public int GetMaterialID(string name)
        {
            return map[name];
        }

        #region IMaterialProvider Members

        public MaterialInfo[] GetAll()
        {
            return this.materials;
        }

        public MaterialInfo Get( int id )
        {
            return this.materials[id];
        }

        public MaterialInfo Get( string name )
        {
            if (!this.map.ContainsKey(name))
                return this.defaultMaterial;
            return this.materials[this.map[name]];
        }

        #endregion
    }
}